package com.autonavi.yunda.yunji.core.service.thirdpartyservice.vo;

import com.autonavi.yunda.yunji.core.model.dto.PartUnitDTO;
import com.autonavi.yunda.yunji.core.vo.response.FalconOptionsVO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.util.stream.Collectors;

import static com.autonavi.yunda.yunji.core.model.constants.PartUnitConstants.FALCON_SOURCE_TYPE;
import static com.autonavi.yunda.yunji.core.model.constants.PartUnitConstants.SOURCE_PREFIX;

@EqualsAndHashCode(callSuper = true)
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class PartVO extends BasePartUnitVO {
    private PartUnitApiDetailVO partDetail;

    public static PartVO from(PartUnitDTO dto, FalconOptionsVO optionsVO, String workNo) {
        PartVO vo = new PartVO();
        if (dto.getYuncangId() != null) {
            vo.setId(Long.valueOf(dto.getYuncangId()));
        }
        vo.setCode(dto.getCode());
        vo.setType("api");
        vo.setVersion(dto.getVersion());
        vo.setName(dto.getTitle());
        vo.setTitle(dto.getTitle());
        vo.setTagIds(dto.getTags().stream().map(TagVO::getId).collect(Collectors.toList()));
        vo.setDescription(optionsVO.getDescription());
        vo.setNotes(optionsVO.getDescription());
        vo.setSourceType(FALCON_SOURCE_TYPE);
        vo.setSourceId(dto.getFalconId());
        vo.setSourceUrl(SOURCE_PREFIX + dto.getFalconId());
        vo.setCreateMember(dto.getCreateUser());
        vo.setUpdateMember(dto.getUpdateUser());
        vo.setTestReportId(dto.getExtraData().getTestId());
        vo.setWorkBdId(dto.getBdId());
        vo.setWorkNo(workNo);
        vo.participant = dto.getUpdateUser();
        vo.setPartDetail(new PartUnitApiDetailVO());
        vo.partDetail.setUri(optionsVO.getUri());
        vo.partDetail.setDomain(optionsVO.getServer());
        vo.partDetail.setFalconUrl(SOURCE_PREFIX + dto.getFalconId());
        vo.partDetail.setFalconId(dto.getFalconId());
        return vo;
    }

}

